package com.yunlankeji.stemcells.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yunlankeji.ganxibaozhijia.R;
import com.yunlankeji.ganxibaozhijia.databinding.AddConent2Binding;
import com.yunlankeji.stemcells.BaseApplication;
import com.yunlankeji.stemcells.entity.Investor;

import java.util.List;

/**
 * @author yang
 */
public class ContentAddAdapter extends RecyclerView.Adapter<ContentAddAdapter.ViewHolder> {

    private List<Investor> investors;
    private AddConent2Binding binding;

    public ContentAddAdapter(List<Investor> investors) {
        this.investors = investors;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        binding = AddConent2Binding.inflate(LayoutInflater.from(BaseApplication.getAppContext()), parent, false);
        return new ViewHolder(binding.getRoot());
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if(position==0){
            holder.imgDeleteImg.setVisibility(View.GONE);
            holder.imgDelete.setVisibility(View.GONE);
        }else {
            holder.imgDeleteImg.setVisibility(View.VISIBLE);
            holder.imgDelete.setVisibility(View.VISIBLE);
        }
        holder.imgDelete.setOnClickListener(v -> {
            investors.remove(position);
            notifyDataSetChanged();
        });
        holder.etName.setText(investors.get(position).getName());
        holder.etTime.setText(investors.get(position).getTime());
        holder.etCost.setText(investors.get(position).getCost());
        holder.etPhone.setText(investors.get(position).getPhone());
        holder.etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                investors.get(position).setName(s.toString().trim());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        holder.etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                investors.get(position).setPhone(s.toString().trim());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        holder.etCost.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                investors.get(position).setCost(s.toString().trim());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        holder.etTime.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                investors.get(position).setTime(s.toString().trim());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public int getItemCount() {
        return investors==null? 0 : investors.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        final ImageView imgDeleteImg;
        final View imgDelete;
        final EditText etTime;
        final EditText etCost;
        final EditText etPhone;
        final EditText etName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imgDeleteImg = itemView.findViewById(R.id.img_delete_img);
            imgDelete = itemView.findViewById(R.id.img_delete);
            etTime = itemView.findViewById(R.id.et_time);
            etCost = itemView.findViewById(R.id.et_cost);
            etPhone = itemView.findViewById(R.id.et_phone);
            etName = itemView.findViewById(R.id.et_name);
        }
    }

}
